import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { UserService } from '../../../service/userService';
import { HttpService } from '../../../service/httpService';
import { NativeService } from '../../../service/NativeService';
import { HistoryPage } from '../history/history';

@Component({
  selector: 'page-transfer',
  templateUrl: 'transfer.html',
})
export class TransferPage {

  private platforms:any;
  private inPlatformModel = '';
  private outPlatformModel = 'main';
  private money:number;
  constructor(public navCtrl: NavController,
              public user:UserService,
              public http:HttpService,
              public nat:NativeService,
              public navParams: NavParams) {
    this.initData();
  }

  initData(){
    let options = [{text:'主钱包',value:'main',disabled:true}];
    let platforms = this.user.platformsCash;
    for (var i=0;i<platforms.length;i++){
      if(i == 0){
        this.inPlatformModel = platforms[i].code;
      }
      let obj = {text:platforms[i].name,value:platforms[i].code,disabled:false};
      options.push(obj);
    }
    this.platforms = [{
      name: 'platform',
      options: options
    }];
  }

  gotoPageAction(){

    this.navCtrl.push(HistoryPage,{title:"转账记录"});

  }

  onPickerChange(e,index){
    if(index == 1){
      this.outPlatformModel = e.platform.value;
      this.inPlatformModel = 'main';
    }else if(index == 2){
      this.inPlatformModel = e.platform.value;
      this.outPlatformModel = 'main';
    }
  }

  onTransferAction(){
    if (!this.outPlatformModel || this.outPlatformModel == ''){
      this.nat.showToast("请选择转入平台");
      return;
    }
    if (!this.inPlatformModel || this.inPlatformModel == ''){
      this.nat.showToast("请选择转出平台");
      return;
    }
    if(this.inPlatformModel == this.outPlatformModel){
      this.nat.showToast("转出和转出不能一样");
      return;
    }
    if(!this.money || this.money <= 0){
      this.nat.showToast("请输入转账金额");
      return;
    }

    let isMainOut = true;
    if (this.outPlatformModel == 'main'){
      isMainOut = true;
    }else {
      isMainOut = false;
    }

    let url = '';
    let map = {
      UserName:this.user.userName,
      GamePlatform: isMainOut ? this.inPlatformModel:this.outPlatformModel,
      Amount:this.money,
      IsDemo:this.user.isDemo
    };

    if (isMainOut){
      url = this.http.baseUrl + '/api/Transfer/DoTransferToGame';
    }else {
      url = this.http.baseUrl + '/api/Transfer/DoTransferFromGame';
    }
    let self = this;
    this.nat.showLoading('转账中...');
    this.http.post(url,map,function (res,err) {
      self.nat.hideLoading();
      if(!err){
        if(res){
          self.nat.showToast("转账成功!");
          let platforms = self.user.platformsCash;
          if(isMainOut){
            self.user.cash -= map.Amount;
          }else {
            self.user.cash += map.Amount;
          }
          for (var i=0;i<platforms.length;i++){
            if(platforms[i].code == map.GamePlatform){
              if(isMainOut){
                platforms[i].money += map.Amount;
              }else {
                platforms[i].money -= map.Amount;
              }
            }
          }
        }
      }
    })
  }

  selectObject(){

  }
}
